package com.hy.example.shared.utils;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

public class TimeUtils {

    public static String formatDuration(Duration duration) {
        return formatDuration(duration, FormatUnit.DAY, FormatUnit.SECOND, FormatType.ZH_CN);
    }

    public static String formatDuration(Duration duration, FormatUnit maxUnit, FormatUnit minUnit, FormatType formatType) {
        long days = 0;
        if (maxUnit.level >= FormatUnit.DAY.level && minUnit.level <= FormatUnit.DAY.level) {
            days = duration.toDays();
            duration = duration.minus(days, ChronoUnit.DAYS);
        }

        long hours = 0;
        if (maxUnit.level >= FormatUnit.HOUR.level && minUnit.level <= FormatUnit.HOUR.level) {
            hours = duration.toHours();
            duration = duration.minus(hours, ChronoUnit.HOURS);
        }

        long minutes = 0;
        if (maxUnit.level >= FormatUnit.MINUTE.level && minUnit.level <= FormatUnit.MINUTE.level) {
            minutes = duration.toMinutes();
            duration = duration.minus(minutes, ChronoUnit.MINUTES);
        }
        long seconds = 0;
        if (maxUnit.level >= FormatUnit.SECOND.level && minUnit.level <= FormatUnit.SECOND.level) {
            seconds = duration.getSeconds();
        }

        StringBuilder sb = new StringBuilder();
        if (days > 0) {
            sb.append(days).append(formatType.day);
        }
        if (hours > 0) {
            sb.append(hours).append(formatType.hour);
        }
        if (minutes > 0) {
            sb.append(minutes).append(formatType.minute);
        }
        if (seconds > 0) {
            sb.append(seconds).append(formatType.second);
        }
        //处理0值
        if (sb.isEmpty()) {
            return "小于1" + FormatType.getFormat(minUnit, formatType);
        }
        return sb.toString();
    }

    @Getter
    @RequiredArgsConstructor
    public enum FormatUnit {

        DAY(5),
        HOUR(4),
        MINUTE(3),
        SECOND(2),
        ;

        private final Integer level;
    }

    @Getter
    @RequiredArgsConstructor
    public enum FormatType {

        ZH_CN("天", "小时", "分钟", "秒"),
        ZH_SHORT("天", "小时", "分钟", "秒"),
        EN_US("day", "hour", "minute", "second"),
        EN_SHORT("d", "h", "m", "s"),
        ;

        private final String day;
        private final String hour;
        private final String minute;
        private final String second;

        public static String getFormat(FormatUnit formatUnit, FormatType formatType) {
            switch (formatUnit) {
                case DAY -> {
                    return formatType.day;
                }
                case HOUR -> {
                    return formatType.hour;
                }
                case MINUTE -> {
                    return formatType.minute;
                }
                case SECOND -> {
                    return formatType.second;
                }
                default -> throw new IllegalArgumentException("位置错误");
            }
        }
    }
}
